package tool.soft

import java.io.File
import command.CommandExec
import dao._
import models.Tables
import org.joda.time.DateTime
import play.api.mvc.{AnyContent, Request}
import play.twirl.api.Html
import shared.Pojo._
import tool.Pojo._
import tool._
import tool.soft.AllSoft.SoftBT
import implicits.Implicits._
import models.Tables._
import play.api.data.FormBinding
import tool.plot.AllPlot._
import tool.plot._
import play.api.i18n.Messages
import upickle.default._

/** Created by yz on 16/9/2020
  */
object ZScoreSoft extends ZScoreSoftT with SoftBT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages
  ): List[FileInfoData] = {
    List(
      FileInfoData("Z_Score.csv", messages("zScoreMatrix")),
      FileInfoData("Z_Score_Heatmap.pdf", messages("zScoreHeatmap"))
    )
  }

  override def produceConfigFile(workspaceDir: File, index: Int)(implicit
      request: Request[AnyContent],
      formBinding: FormBinding
  ) = {
    data.lines().toTxtFile(Tool.getCalculateConfigTxtFile(workspaceDir))
    plot.ZScoreHeatmap.initConfigFile(workspaceDir, idStr = "")
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val groupFile = new File(workspaceDir, "group.txt")
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val command =
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "z-score_calculate.R").unixPath}
           """.stripMargin
    val plotCommands =List(
      plot.ZScoreHeatmap.getCommand(workspaceDir, idStr = ""),
    )
    List(CommandData(workspaceDir, List(command):::plotCommands))
  }

  override def getHtml(missionName: String, missionId: Option[Int])(implicit
      request: Request[AnyContent],
      messages: Messages
  ): Html = {
    views.html.user.soft.zScore(missionName, missionId)
  }

  override def getArg(implicit
      request: Request[AnyContent],
      messages: Messages,
      formBinding: FormBinding
  ): ujson.Value = {
    ujson.Obj()
  }

  override def getMission(implicit
      request: Request[AnyContent],
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages,
      formBinding: FormBinding
  ): Tables.MissionRow = {
    val initInputs = List(data.file)
    val argJson = getArg
    MissionRow(
      id = 0,
      missionName = s"${data.missionName}",
      kind = nameStr,
      userId = userId,
      startTime = new DateTime(),
      state = "preparing",
      arg = argJson,
      input = initInputs
    )
  }

  override def produceInputFile(
      workspaceDir: File
  )(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceRawMetaboliteFile(userId, data.file, rawDataFile)
  }

  override def data(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    FormTool.zScoreForm.bindFromRequest().get
  }

  override def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.mission.zScoreResult(missionId)
  }

}
